// pages/address/modify/modify.js
var app = getApp()
const $api = require('../../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示显示    0表示不显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '添加地址', //导航栏 中间的标题
      bg: '#FFFFFF', //导航背景
    },
    buyer_name: '', //收件人姓名
    buyer_mobile: '', //收件人电话
    province_id: '', //收件人省id
    city_id: '', //收件人市id
    area_id: '', //收件人区id
    address: '', //收件人详细地址
    is_default: 0, //是否是默认地址 1是 0 不是
    id: '', //地址id 修改时必传
    addressSHQ: ['省', '市', '区'],
    regionIndex: [0, 0, 0], //地址省市区默认下标
    jf:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var _this = this;
    _this.setData({
      loadModal: true
    })
    //获取地址
    $api.getArea({
      login_token: wx.getStorageSync('login_token'),
    }).then((res) => {
      _this.setData({
        addressJson: res.data.data,
        loadModal: false
      })
      //修改地址
      if (wx.getStorageSync('address')) {
        var address = wx.getStorageSync('address');
        _this.setData({
          buyer_name: address.buyer_name,
          buyer_mobile: address.buyer_mobile,
          province_id: address.province_id,
          city_id: address.city_id,
          area_id: address.area_id,
          address: address.address,
          addressSHQ: address.addressSHQ
        })
      }
      _this.setData({
        step: options.step,
        order: options.order,
        order_id: options.orderid,
        dzy:options.dzy,
        status:options.status,
        
      })
      if (options.jf) {
        _this.setData({
          jf: options.jf
        })
      }

      if (options.order == 1) {
        _this.setData({
          loadModal: true,
        })
        $api.getOrderDetail({
          login_token: wx.getStorageSync('login_token'),
          order_id: options.orderid
        }).then((res) => {
          _this.setData({
            buyer_name: res.data.data.order_info.buyer_name,
            buyer_mobile: res.data.data.order_info.buyer_mobile,
            province_id: res.data.data.order_info.province_id,
            city_id: res.data.data.order_info.city_id,
            area_id: res.data.data.order_info.area_id,
            address: res.data.data.order_info.address,
            addressSHQ: [res.data.data.order_info.province_name, res.data.data.order_info.city_name, res.data.data.order_info.area_name],
            loadModal: false
          })
        }).catch((err) => {
          _this.setData({
            loadModal: false
          })
        })
      }
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
      wx.showToast({
        title: '地址获取失败',
        icon: 'none',
        duration: 2000
      })
    })


  },
  //地址弹出隐藏
  hideModal(e) {
    this.setData({
      modalName: null,
      regionIndex: [0, 0, 0]
    })
  },
  //地点底部弹出
  onSHG(e) {
    var _this = this;
    _this.setData({
      modalName: e.currentTarget.dataset.target,
    })
  },
  //滑动时获取地址index
  onAddressIndex(e) {
    console.log(e)
    var _this = this;
    _this.setData({
      regionIndex: e.detail.value
    })
  },
  //确认地址
  confirmAddress() {
    var _this = this;
    var regionIndex = _this.data.regionIndex;
    _this.setData({
      ['addressSHQ[0]']: _this.data.addressJson[regionIndex[0]].provinceName,
      province_id: _this.data.addressJson[regionIndex[0]].provinceId,
      ['addressSHQ[1]']: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].cityName,
      city_id: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].cityId,
      ['addressSHQ[2]']: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].region[regionIndex[2]].regionName,
      area_id: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].region[regionIndex[2]].regionId,
      modalName: null,
      regionIndex: [0, 0, 0]
    })
  },
  //获取收件人姓名
  onBuyerName(e) {
    this.setData({
      buyer_name: e.detail.value
    })
  },
  //获取收件人手机
  onBuyerMobile(e) {
    this.setData({
      buyer_mobile: e.detail.value
    })
  },
  //获取收件人详细地址
  onAddress(e) {
    this.setData({
      address: e.detail.value
    })
  },
  //用户添加 / 修改收货地址
  onEditUserAddress() {
    var _this = this;
    var buyer_name = _this.data.buyer_name;
    var buyer_mobile = _this.data.buyer_mobile;
    var province_id = _this.data.province_id;
    var city_id = _this.data.city_id;
    var area_id = _this.data.area_id;
    var address = _this.data.address;
    var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; //11位手机号码正则
    if (buyer_name == '') {
      wx.showToast({
        title: '请添加收货人姓名',
        icon: 'none',
        duration: 2000
      })
    } else if (buyer_mobile == '') {
      wx.showToast({
        title: '请添加手机号码',
        icon: 'none',
        duration: 2000
      })
    } else if (!reg_tel.test(buyer_mobile)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000
      })
    } else if (province_id == '') {
      wx.showToast({
        title: '请选择地区',
        icon: 'none',
        duration: 2000
      })
    } else if (address == '') {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.setStorageSync('address', {
        buyer_name: buyer_name,
        buyer_mobile: buyer_mobile,
        addressSHQ: _this.data.addressSHQ,
        province_id: province_id,
        city_id: city_id,
        area_id: area_id,
        address: address
      })
      if (_this.data.order == 1) {
        _this.setData({
          loadModal: true
        })
        $api.editOrderAddress({
          login_token: wx.getStorageSync('login_token'),
          order_id: _this.data.order_id,
          buyer_name: buyer_name,
          buyer_mobile: buyer_mobile,
          addressSHQ: _this.data.addressSHQ,
          province_id: province_id,
          city_id: city_id,
          area_id: area_id,
          address: address
        }).then((res) => {
          _this.setData({
            loadModal: false
          })
          wx.navigateBack({
            delta: 1,
          })
        }).catch((err) => {
          _this.setData({
            loadModal: false
          })
        })
      } else {
        var jf = _this.data.jf;
        if (jf == 1) {
          wx.navigateBack({
            delta: 1,
          })
        } else {
          if (wx.getStorageSync('pay') == 1) {
            wx.navigateBack({
              delta: 1,
            })
          } else {
            wx.redirectTo({
              url: '/pages/reading/reading?type=1&step=' + _this.data.step+'&dzy='+_this.data.dzy  + '&status=' + _this.data.status,
            })
          }
        }

      }
    }
  },

})